/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.util;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 作者：韩天
 * 创建时间：2011-8-2
 * 类描述：读取excel文件的工具类
 */
public class ExcelReader {
    private String filePath = "";//excel表格所在的路径
    private int excelVersion;

    public ExcelReader(String filePath) throws ExcelVersionErrorException {
        this.filePath = filePath;
        this.excelVersion = parseExcelVersion(filePath);
    }
    
    /**
     * 方法描述：读取excel表格中的数据
     * param：location: sheet、row、column——sheet号，行，列
     * return：num——excel表中相应位置的内容
    */
    public double readNumericCell(int sheetIndex, int rowIndex, int columnIndex){
        switch(excelVersion){
            case 2003:  
                return read2003(sheetIndex, rowIndex, columnIndex);
                
            case 2007:  
                return read2007(sheetIndex, rowIndex, columnIndex);
            default:
                return 0;
        }
        
    }
    
    private double read2003(int sheetIndex, int rowIndex, int columnIndex){
        POIFSFileSystem fs = null;
        HSSFWorkbook wb = null;
        try{ 
            fs=new POIFSFileSystem(new FileInputStream(filePath));
            wb = new HSSFWorkbook(fs);
        } catch (IOException e) {    
            e.printStackTrace();    
        }
        HSSFSheet sheet = wb.getSheetAt(sheetIndex);  
        HSSFRow row = sheet.getRow(rowIndex);
        HSSFCell cell = row.getCell(columnIndex); 
        double num = cell.getNumericCellValue();
        
        return num; 
    }
    
    private double read2007(int sheetIndex, int rowIndex, int columnIndex){
        XSSFWorkbook xwb = null;
        try {
            xwb = new XSSFWorkbook(filePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }  
        XSSFSheet sheet = xwb.getSheetAt(sheetIndex);  
        XSSFRow row = sheet.getRow(rowIndex);
        XSSFCell cell = row.getCell(columnIndex);
        double num = cell.getNumericCellValue();
        return num;
    }
    
    /**
     * 方法描述：根据文件路径的后缀，解析企业传的excel表格为哪个版本
     * param：filePath——excel表格路径
     * return：2003/2007——支持读取的excel版本
    */
    private int parseExcelVersion(String filePath) throws ExcelVersionErrorException {
        String[] subStr = filePath.split("\\.");
        if(subStr[1].equals("xls")){
            return 2003;
        }else if(subStr[1].equals("xlsx")){
            return 2007;
        }else{
            throw new ExcelVersionErrorException();
        }
    }
    
    
}
